package com.xdog.web3.controller.market;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.web3.domain.price.CandlesQuery;
import com.xdog.web3.domain.price.PriceRequest;
import com.xdog.web3.domain.price.TradesQuery;
import com.xdog.web3.exception.Web3ApiException;
import com.xdog.web3.service.IWeb3MarketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Web3行情数据控制器
 */
@Tag(name = "Web3行情数据", description = "Web3行情相关接口")
@RestController
@RequestMapping("/web3/market")
public class MarketController {

    @Resource
    private IWeb3MarketService web3MarketService;

    /**
     * 获取代币价格
     */
    @Operation(summary = "获取代币价格", description = "获取代币价格")
    @PostMapping("/price")
    @SaIgnore
    public R<JSONObject> getPrice(@RequestBody List<PriceRequest> priceRequests) {
        try {
            JSONObject result = web3MarketService.getPrice(priceRequests);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币价格失败: " + e.getMessage());
        }
    }

    /**
     * 获取交易数据
     */
    @Operation(summary = "获取交易数据", description = "获取交易数据")
    @GetMapping("/trades")
    @SaIgnore
    public R<JSONObject> getTrades(TradesQuery query) {
        try {
            JSONObject result = web3MarketService.getTrades(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取交易数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取K线数据
     */
    @Operation(summary = "获取K线数据", description = "获取K线数据")
    @GetMapping("/candles")
    @SaIgnore
    public R<JSONObject> getCandles(CandlesQuery query) {
        try {
            JSONObject result = web3MarketService.getCandles(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取K线数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取历史K线数据
     */
    @Operation(summary = "获取历史K线数据", description = "获取历史K线数据")
    @GetMapping("/historical-candles")
    @SaIgnore
    public R<JSONObject> getHistoricalCandles(CandlesQuery query) {
        try {
            JSONObject result = web3MarketService.getHistoricalCandles(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取历史K线数据失败: " + e.getMessage());
        }
    }
}